package com.itheima.reggie.controller;

import com.itheima.reggie.common.Page;
import com.itheima.reggie.entity.Result;
import com.itheima.reggie.entity.SetmealDto;
import com.itheima.reggie.service.SetmealService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
public class SetmealController {

    @Autowired
    private SetmealService setmealService ;

    @PostMapping("/setmeal")
    @ResponseBody
    @CacheEvict(value = "SETMEAL" , allEntries = true)
    public Result addSetmeal(@RequestBody SetmealDto setmealDto , HttpSession httpSession ){
        Long employee = (Long) httpSession.getAttribute("employee");
        setmealDto.setCreateUser(employee);
        setmealDto.setUpdateUser(employee);
        setmealService.addSetmeal(setmealDto);
        return Result.success(null);
    }

    //套餐页面
    @GetMapping("/setmeal/page")
    @ResponseBody
    public Result page( Integer page , Integer pageSize , String name ){
        Result result = setmealService.page( page , pageSize , name );
        return result;
    }

    @DeleteMapping("/setmeal")
    @ResponseBody
    @CacheEvict( value = "SETMEAL" , allEntries = true )
    public Result delete ( Long[] ids ){
        return  setmealService.delete(ids);
    }

    //根据分类的id查询有哪些套餐
    @RequestMapping("/setmeal/list")
    @Cacheable(value = "SETMEAL" , key = "'ONSALE' + #categoryId" , unless = "#result.data.size()==0")
    @ResponseBody
    public Result findByCategoryId ( String  categoryId ){
        return setmealService.findByCategoryId(categoryId);
    }

}
